package com.yc.bank;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
// MyBatis-Plus 扫包注解 ==> Mapper 接口 or XML
@MapperScan("com.yc.bank.mapper")
public class BankConfig {

    @Bean
    DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///test");
        dataSource.setUsername("root");
        dataSource.setPassword("a");
        return dataSource;
    }

    // 会话工厂bean
    @Bean
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        MybatisSqlSessionFactoryBean ssfb = new MybatisSqlSessionFactoryBean();
        ssfb.setDataSource(dataSource);
//        org.apache.ibatis.session.Configuration configuration =
//                new org.apache.ibatis.session.Configuration();
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setLogImpl(Log4jImpl.class);
        // 下面的两个方法互斥
        //ssfb.setConfigLocation("mybatis.xml 的类路径");
        ssfb.setConfiguration(configuration);
        MybatisPlusInterceptor mpi = new MybatisPlusInterceptor();
        // 分页拦截器
        final PaginationInnerInterceptor pii = new PaginationInnerInterceptor();
        pii.setDbType(DbType.MYSQL);
        // 数据库方言对象 ==> hibernate
        pii.setDialect(new MySqlDialect());
        mpi.addInnerInterceptor(pii);
        ssfb.setPlugins(mpi);

        // pageHelper 插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        // 设置方言, 不设置方言, 让其自动检测
        //pageInterceptor.setProperties("dialect","...MySQL..");
        ssfb.setPlugins(pageInterceptor);

        return ssfb;
    }

    // MyBatis-Plus 拦截器
    @Bean
    MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mpi = new MybatisPlusInterceptor();
        // 分页拦截器
        final PaginationInnerInterceptor pii = new PaginationInnerInterceptor();
        pii.setDbType(DbType.MYSQL);
        // 数据库方言对象 ==> hibernate
        pii.setDialect(new MySqlDialect());
        mpi.addInnerInterceptor(pii);
        return mpi;
    }


}
